<%@ page contentType="text/html;charset=utf-8" %>

<%
	String context = request.getContextPath();

	//文件保存的相对路径
	String saveFilePath = request.getParameter("saveFilePath");
	if(saveFilePath == null) saveFilePath = "/static/fileupload/data";
	
	//元素名称
	String fieldName = request.getParameter("fieldName");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META content="text/html; charset=UTF-8" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>

<SCRIPT LANGUAGE="JavaScript" src="js/prototype.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" src="js/waitbar.js"></SCRIPT>

</HEAD>

<BODY bgColor=#e6e6e6>
<BR>


<iframe name="actionFrame" id="actionFrame" src="" width=0 height=0></iframe>

<FORM action="<%=context%>/fileupload?saveFilePath=<%=saveFilePath%>" encType="multipart/form-data" method="post">

<span id="waitSpan"></span>
<br>

<TABLE>
	<TBODY>
		<TR>
			<TD><FONT color=#000000 face=helv,helvetica size=2>&nbsp;&nbsp;选择文件
			：&nbsp;</FONT>&nbsp;&nbsp;<INPUT size=60 type=file name="file_0"></TD>
			<TD align=right><INPUT type=button value=上传 name="send" onclick="doUpload()"></TD>
		</TR>
	</TBODY>
</TABLE>
</FORM>
</BODY>

<script>

//var waitbar = new Waitbar("toUpload","<%=context%>");
//waitbar.setTitle("数据正在上传，请稍候……");

function doUpload(){
	document.forms[0].target = 'actionFrame'; 
	document.forms[0].submit();

	//waitbar.wait();
	getState();
}
function getState(){
	var url = "<%=context%>/fileupload?action=getState";
	var ajax = new Ajax.Request(url, {method: "post" , postBody: "", onComplete: process} );
}
function process(request){
	var result = request.responseText; 
	if(result.indexOf("complete")!=-1){
		var start = result.indexOf("saveFilePath") + "saveFilePath:".length;
		var end = result.indexOf(",complete");
		var serverSaveFilePath = result.substring(start,end);
		alert("文件上传成功！");
		document.getElementById("waitSpan").innerHTML = "";
		//waitbar.finish();

		//将路径返回到父窗口
		window.opener.document.getElementById("<%=fieldName%>").value = serverSaveFilePath;
		window.close();
	}else if(result.indexOf("error")!=-1){
		alert("文件上传出错！");
		document.getElementById("waitSpan").innerHTML = "";
		//waitbar.finish();
	}else{
		//显示当前进度,继续读取进度
		setTimeout(getState,1000);
		//waitbar.resetTitle(result);
		document.getElementById("waitSpan").innerHTML = result;
	}
}

</script>

</HTML>



